<!--
  speedy-vision.js
  GPU-accelerated Computer Vision for JavaScript
  Copyright 2020-2024 Alexandre Martins <alemartf(at)gmail.com>

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  
      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

  median-filter.html
  Blurring a video with a median filter
-->
<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="description" content="speedy-vision.js: GPU-accelerated Computer Vision for JavaScript">
        <meta name="author" content="Alexandre Martins">
        <title>Blurring a video with a median filter</title>
        <link href="demo-base.css" rel="stylesheet">
        <script src="demo-base.js"></script>
        <script src="../dist/speedy-vision.js"></script>
    </head>
    <body>
        <h1>Median filter</h1>
        <form autocomplete="off">
            <div>
                <label for="kernel-size">Kernel size:</label>
                <select id="kernel-size">
                    <option value="3" selected>3x3</option>
                    <option value="5">5x5</option>
                    <option value="7">7x7</option>
                </select>
            </div>
            &nbsp;&nbsp;&nbsp;
            <div>
                <label for="speed-slider">Video speed</label>
                <input type="range" id="speed-slider" min="0.10" max="2" value="1" step="0.01">
            </div>
        </form>
        <div>
            <span id="status"></span>
            <canvas id="canvas-demo"></canvas>
        </div>
        <video id="video"
            poster="../assets/loading.jpg"
            loop muted autoplay playsinline hidden
            title="Free video by Wendy Laplume, https://www.pexels.com/pt-br/video/passaro-ave-passarinho-concreto-4168986/">
                <source src="../assets/bird-walking.webm" type="video/webm" />
                <source src="../assets/bird-walking.mp4" type="video/mp4" />
        </video>
        <script>
window.onload = async function()
{
    /*

    This is our pipeline:

    Image  ---> Convert to ---> Median ---> Image
    Source      greyscale       Blur        Sink
                |
                |
                +-------------> Image
                                Sink

    */

    // Load a video
    const video = document.getElementById('video');
    const media = await Speedy.load(video);

    // Setup the pipeline
    const pipeline = Speedy.Pipeline();
    const source = Speedy.Image.Source();
    const sink1 = Speedy.Image.Sink('median');
    const sink2 = Speedy.Image.Sink('greyscale');
    const greyscale = Speedy.Filter.Greyscale();
    const median = Speedy.Filter.MedianBlur();

    source.media = media;
    median.kernelSize = Speedy.Size(3,3);

    source.output().connectTo(greyscale.input());
    greyscale.output().connectTo(median.input());
    median.output().connectTo(sink1.input());
    greyscale.output().connectTo(sink2.input());

    pipeline.init(source, sink1, sink2, greyscale, median);

    // Setup the input controls
    const kernelSize = document.getElementById('kernel-size');
    const speedSlider = document.getElementById('speed-slider');
    speedSlider.oninput = () => video.playbackRate = speedSlider.value;
    kernelSize.oninput = () => median.kernelSize = Speedy.Size(kernelSize.value, kernelSize.value);

    // Main loop
    (function() {
        const canvas = setupCanvas('canvas-demo', media.width * 2, media.height, video.title);
        let median = null, greyscale = null, frameReady = false;

        async function update()
        {
            const result = await pipeline.run();
            median = result.median;
            greyscale = result.greyscale;

            frameReady = true;
            setTimeout(update, 1000 / 60);
        }
        update();

        function render()
        {
            if(frameReady) {
                renderMedia(median, canvas, 0, 0);
                renderMedia(greyscale, canvas, median.width, 0);
            }

            frameReady = false;
            requestAnimationFrame(render);
        }
        render();

        setInterval(renderStatus, 200);
    })();
}
        </script>
        <section id="speedy-vision">
            <span>Powered by <a href="https://github.com/alemart/speedy-vision" target="_blank">speedy-vision.js</a></span>
            <a href="https://github.com/sponsors/alemart" target="_blank" class="button">
                <img alt="GitHub Sponsors" src="https://img.shields.io/github/sponsors/alemart?style=for-the-badge&logo=github&label=Sponsor&labelColor=royalblue&color=gold">
            </a>
        </section>
    </body>
</html>